﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MyMes.Infrastructure;
using MyMES.Dto;
using MyMES.IServices.Productionmaterialsment;
using VOL.Core.Extensions;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.Core;
using VOL.Entity.DomainModels.Repository;

namespace MyMES.Services.Productionmaterialsment
{
    public class ProductionmaterialsServices : IProductionmaterialsServices
    {
        private readonly IBaseRepository<Productionmaterials> _productionmaterialsRepository;
        private readonly IBaseRepository<Workorder> _workorderRepository;
       

        public ProductionmaterialsServices(IBaseRepository<Productionmaterials> productionmaterialsRepository, IBaseRepository<Workorder> workorderRepository)
        {
            _productionmaterialsRepository = productionmaterialsRepository;
            _workorderRepository = workorderRepository;
           
        }

        public async Task<int> CreateProductionmaterials(Productionmaterials productionmaterials)
        {
           
           
            await _productionmaterialsRepository.AddAsync(productionmaterials);
            
            return 1;
        }

        public async Task<int> UpdProductionmaterials(Productionmaterials productionmaterials)
        {
            await _productionmaterialsRepository.UpdateAsync(productionmaterials);

            return 1;
        }

        public async Task<PageGridData<Productionmaterials>> QueryProductionmaterials(ProductionmaterialsDTO productionmaterialsDTO, int PageIndex, int PageSize)
        {
            PageGridData<Productionmaterials> pageData = new PageGridData<Productionmaterials>();
            // 构建查询
            var list = _productionmaterialsRepository.GetAll();
            // 使用WhereIf方法进行条件查询
            list = list.WhereIF(!string.IsNullOrEmpty(productionmaterialsDTO.MateCode),
                                 x => x.MateCode.Contains(productionmaterialsDTO.MateCode));
            list = list.WhereIF(!string.IsNullOrEmpty(productionmaterialsDTO.MateName),
                                x => x.MateName.Contains(productionmaterialsDTO.MateName));
            list = list.WhereIF(!string.IsNullOrEmpty(productionmaterialsDTO.MateType),
                                x => x.MateType==productionmaterialsDTO.MateType);
            list = list.WhereIF(!string.IsNullOrEmpty(productionmaterialsDTO.ProductionOrder),
                                x => x.ProductionOrder.Contains(productionmaterialsDTO.ProductionOrder));


            // 获取总记录数
            pageData.total = await list.CountAsync();

            // 分页处理
            var data = await list.OrderByDescending(a => a.Id)
                                 .Skip((PageIndex - 1) * PageSize)
                                 .Take(PageSize)
                                 .ToListAsync();

            pageData.rows = data;

            return pageData;
        }

        public async Task<PageGridData<Workorder>> QueryWorkorder(WorkorderDTO workorderDTO, int PageIndex, int PageSize)
        {
            PageGridData<Workorder> pageData = new PageGridData<Workorder>();
            // 构建查询
            var list = _workorderRepository.GetAll();
            // 使用WhereIf方法进行条件查询
            list = list.WhereIF(!string.IsNullOrEmpty(workorderDTO.WorkOrderCode),
                                 x => x.WorkOrderCode.Contains(workorderDTO.WorkOrderCode));
            list = list.WhereIF(!string.IsNullOrEmpty(workorderDTO.WorkOrderName),
                                x => x.WorkOrderName.Contains(workorderDTO.WorkOrderName));
            list = list.WhereIF(!string.IsNullOrEmpty(workorderDTO.ProductNumber),
                                x => x.ProductNumber.Contains(workorderDTO.ProductNumber));
            list = list.WhereIF(!string.IsNullOrEmpty(workorderDTO.ProductName),
                                x => x.ProductName.Contains(workorderDTO.ProductName));
            list = list.WhereIF(!string.IsNullOrEmpty(workorderDTO.ClientName),
                                x => x.ClientName.Contains(workorderDTO.ClientName));


            // 获取总记录数
            pageData.total = await list.CountAsync();

            // 分页处理
            var data = await list.OrderByDescending(a => a.Id)
                                 .Skip((PageIndex - 1) * PageSize)
                                 .Take(PageSize)
                                 .ToListAsync();

            pageData.rows = data;

            return pageData;
        }
    }
}
